package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.PurchaseStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author atguigu-mqx
 * @ClassName AlbumPurchaseStrategy
 * @description: TODO
 * @date 2024年04月29日
 * @version: 1.0
 */
@Service("1001")
@Slf4j
public class AlbumPurchaseStrategy implements PurchaseStrategy {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;


    @Override
    public void purchase(UserPaidRecordVo userPaidRecordVo) {
        //	判断当前用户是否已经记录了购买专辑信息.
        UserPaidAlbum userPaidAlbum = this.userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getAlbumId, userPaidRecordVo.getItemIdList().get(0)).eq(UserPaidAlbum::getUserId, userPaidRecordVo.getUserId()));
        //	判断
        if (null != userPaidAlbum) {
            //	说明已经记录过。
            log.info("当前用户{} 已购买过专辑Id{}", userPaidRecordVo.getUserId(), userPaidRecordVo.getItemIdList().get(0));
            return;
        }
        //	记录信息.
        userPaidAlbum = new UserPaidAlbum();
        //	赋值
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
        userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
        this.userPaidAlbumMapper.insert(userPaidAlbum);
    }
}
